using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class tblichsuviDAL
    {
        private SqlConnection cn = null;
        public tblichsuviDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tblichsuviDAL(tblichsuviDAL lichsuvi )
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tblichsuvi lichsuvi)
        {
            SqlCommand cm = new SqlCommand("lichsuvi_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@manguoidung", lichsuvi .Manguoidung  );
            cm.Parameters.AddWithValue("@maloaigiaodich", lichsuvi .Maloaigiaodich );
            cm.Parameters.AddWithValue("@sotien", lichsuvi .Sotien );
            cm.Parameters.AddWithValue("@ngaygio", lichsuvi .Ngaygio );
            cm.ExecuteNonQuery();
        }
        public void Update(tblichsuvi lichsuvi)
        {
            SqlCommand cm = new SqlCommand("lichsuvi_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mathanhtoan", lichsuvi .Magiaodich  );
            cm.Parameters.AddWithValue("@manguoidung", lichsuvi .Manguoidung );
            cm.Parameters.AddWithValue("@maloaithanhtoan", lichsuvi .Maloaigiaodich );
            cm.Parameters.AddWithValue("@sotien", lichsuvi .Sotien );
            cm.Parameters.AddWithValue("@ngaygio", lichsuvi .Ngaygio );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("lichsuvi_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mathanhtoan", iD);
            cm.ExecuteNonQuery();
        }
        public tblichsuvicollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("lichsuvi_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tblichsuvicollection lichsuviCollection = new tblichsuvicollection();
            while (dr.Read())
            {
                tblichsuvi  lichsuvi = new tblichsuvi ();
                lichsuvi.Magiaodich   = int.Parse(dr.GetValue(0).ToString());
                lichsuvi.Manguoidung  = int.Parse(dr.GetValue(1).ToString());
                lichsuvi.Maloaigiaodich= int.Parse(dr.GetValue(2).ToString());
                lichsuvi.Sotien= int.Parse(dr.GetValue(3).ToString());
                lichsuvi.Ngaygio = DateTime.Parse(dr.GetValue(4).ToString());
                lichsuviCollection.Add(lichsuvi);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return lichsuviCollection ;
        }
    }
}
